package com.ywy.config;

import com.ywy.chat.handler.MsgHandlerFactory;
import com.ywy.chat.server.ChatServerStarter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * 即时通讯配置
 *
 * @author ywy
 * @date 2021/12/31 14:03
 */
@Slf4j
@Configuration
public class ChatServerConfig {
    @Value("${chat.server.port}")
    private Integer imPort;

    @Bean
    public ChatServerStarter imServerStarter() {
        try {
            // 初始化WsServerStarter
            ChatServerStarter imServerStarter = new ChatServerStarter(imPort);
            // 启动tio服务
            imServerStarter.start();

            // 初始化消息处理器工厂
            MsgHandlerFactory.init();

            log.info("chat server started!");

            return imServerStarter;
        } catch (IOException e) {
            e.printStackTrace();
            log.error("chat server start fail!", e);
        }
        return null;
    }
}
